//index.js
//获取应用实例
const app = getApp()
const api = require('../../../api/index.js')

let baseURL1 = 'https://www.didigolf.top/assets/dingchang/';
Page({
  data: {

    hide: true,
    tab: [{
      id: 1,
      name: "海南"
    }, {
      id: 2,
      name: "价格"
    }, {
      id: 3,
      name: "距离",
      checked: true
    }],
    param: {} // 全局定义一个初始值
  },

  onLoad: function() {
    var that = this // 变量名
    wx.getLocation({ // 获取当前的地理位置
      type: 'wgs84', // 默认值 wgs84 返回 gps 坐标
      success: function(res) { // 调用成功返回的函数
        console.log(res)
        var param = that.data.param // 定义局部 param = 全局 param
        console.log(param) // 
        param.latitude = res.latitude
        param.longitude = res.longitude
        param.distance = "distance"
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        that.getList(param)
      }
    })
  },
  foucus: function (e) {
    this.setData({
      hide: false
    })
  },
  blur: function (e) {
    this.setData({
      hide: true
    })
  },
  search: function (e) {
    console.log(e)
    var param = this.data.param
    param.name = e.detail.value
    this.getList(param)
  },
  tab: function(e) {
    var id = e.currentTarget.id
    var param = {}
    var tab = this.data.tab
    tab.forEach(function(element) {
      if (element.id == id) {
        element.checked = true
      } else {
        element.checked = false
      }
    })
    this.setData({
      tab: tab
    })
    if (id == 1) {

    } else if (id == 2) {
      param.price = "didi"
      param.latitude = this.data.latitude
      param.longitude = this.data.longitude
    } else {
      param.distance = "distance"
      param.latitude = this.data.latitude
      param.longitude = this.data.longitude
    }
    this.getList(param)
    console.log(param)
  },
  log: function(e) {
    var id = e.currentTarget.id; //这个是传过来的参数，e_id
    wx.navigateTo({
      url: '../logs/logs?ind=' + id,
    })
  },
  getList: function(data) {
    var that = this
    data.token = wx.getStorageSync('token').token
    api.getList(data, (res) => {
      console.log(res)
      that.setData({
        result: res.data.result
      })
    })
  }
  
})